/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ps.server;

import com.ps.server.classes.categories;
import java.util.ArrayList;
import java.util.Vector;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

/**
 *
 * @author Ricardo
 */
public class CategoryLibraryXMLReader extends DefaultHandler {

    Vector nodos = new Vector();
    String contenido = "";
    String language = "";
    private ArrayList<langTemp> descriptionLanguage;
    private ArrayList categories;
    private ArrayList products;
    private boolean associations;
    private categories nodoActualCategories = new categories();
    String resource;
    private langTemp tem;
    //private clsProduct nodoActual = new clsProduct();

    public CategoryLibraryXMLReader(Vector nodos) {
        this.nodos = nodos;
    }

    public CategoryLibraryXMLReader() {
        this.associations = false;
    }
    /*
     * Esta funcion el llamada cuando se produce el evento de ver una nueva
     * etiqueta
     */

    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes) {

        if ("category".equals(qName) && this.associations == false) {
            nodoActualCategories = new categories();
        }
        if ("name".equals(qName)) {
            this.tem = new langTemp();
            this.descriptionLanguage = new ArrayList();
        }
        if ("link_rewrite".equals(qName)) {
            this.tem = new langTemp();
            this.descriptionLanguage = new ArrayList();
        }
        if ("meta_title".equals(qName)) {
            this.tem = new langTemp();
            this.descriptionLanguage = new ArrayList();
        }
        if ("meta_description".equals(qName)) {
            this.tem = new langTemp();
            this.descriptionLanguage = new ArrayList();
        }
        if ("meta_keywords".equals(qName)) {
            this.tem = new langTemp();
            this.descriptionLanguage = new ArrayList();
        }
        if ("description".equals(qName)) {
            this.tem = new langTemp();
            this.descriptionLanguage = new ArrayList();
        }

        if ("language".equals(qName)) {
            this.tem.setId(attributes.getValue("id"));
            //this.short_description
            // this.idlang=attributes.getValue("id");
            //this.meta_keywords = new ArrayList();
        }
        if ("associations".equals(qName)) {
            this.associations = true;
        }
        if ("categories".equals(qName) && this.associations == false && this.categories == null && this.products == null) {
            this.resource = qName;
        } else if ("categories".equals(qName) && this.associations == true && this.categories == null && this.products == null) {
            this.categories = new ArrayList();
        }
        if ("products".equals(qName) && this.associations == true && this.categories != null && this.products == null) {
            this.products = new ArrayList();
        }
    }

    /*
     * Esta funcion es llamada cuando ve el contenido de una etiqueta
     */
    @Override
    public void characters(char buf[], int offset, int len)
            throws SAXException {
        contenido = new String(buf, offset, len);

    }
    /*
     * y esta al llegar al final
     */

    @Override
    public void endElement(String uri, String localName, String qName) {
        if (this.resource.equals("categories")) {
            resourceCategories(qName);
        }
    }

    void resourceCategories(String qName) {
        if ("id".equals(qName) && this.associations == false && this.categories == null && this.products == null) {
            // aqui se ya se sabe  el orden de  inicializacion de las variables.
            this.nodoActualCategories.setId(contenido);
        } else if ("id".equals(qName) && this.associations == true && this.categories != null && this.products == null) {
            categories.add(contenido);
        } else if ("id".equals(qName) && this.associations == true && this.categories != null && this.products != null) {
            this.products.add(contenido);
        }
//            if (this.associations == true && this.categories != null && this.products == null ) {
//                categories.add(contenido);
//            } else if (this.associations == true && this.products != null) {
//                products.add(contenido);
//            } else {
//                nodoActualCategories.setId(contenido);
//            }
//        }
        if ("id_parent".equals(qName)) {
            nodoActualCategories.setId_parent(contenido);
        }
        if ("active".equals(qName)) {
            nodoActualCategories.setActive(contenido);
        }
        if ("name".equals(qName)) {
            String[][] shortString = new String[this.descriptionLanguage.size()][2];
            for (int x = 0; this.descriptionLanguage.size() > x; x++) {
                shortString[x][0] = this.descriptionLanguage.get(x).getId();
                shortString[x][1] = this.descriptionLanguage.get(x).getValue();
                // shortString[x][0]=this.idlang;
            }
            this.descriptionLanguage.clear();
            nodoActualCategories.setName(shortString);
//            nodoActualCategories.setName(contenido);
        }

        if ("link_rewrite".equals(qName)) {
            String[][] shortString = new String[this.descriptionLanguage.size()][2];
            for (int x = 0; this.descriptionLanguage.size() > x; x++) {
                shortString[x][0] = this.descriptionLanguage.get(x).getId();
                shortString[x][1] = this.descriptionLanguage.get(x).getValue();
                // shortString[x][0]=this.idlang;
            }
            this.descriptionLanguage.clear();
            nodoActualCategories.setLink_rewrite(shortString);
        }

        if ("language".equals(qName)) {
            langTemp t = new langTemp();
            t.setId(this.tem.getId());
            t.setValue(contenido);
            this.descriptionLanguage.add(t);

            //nodoActualManufacturers.setId(contenido);
        }

        if ("meta_title".equals(qName)) {
            String[][] shortString = new String[this.descriptionLanguage.size()][2];
            for (int x = 0; this.descriptionLanguage.size() > x; x++) {
                shortString[x][0] = this.descriptionLanguage.get(x).getId();
                shortString[x][1] = this.descriptionLanguage.get(x).getValue();
                // shortString[x][0]=this.idlang;
            }
            this.descriptionLanguage.clear();
            nodoActualCategories.setMeta_title(shortString);
        }
        if ("meta_description".equals(qName)) {
            String[][] shortString = new String[this.descriptionLanguage.size()][2];
            for (int x = 0; this.descriptionLanguage.size() > x; x++) {
                shortString[x][0] = this.descriptionLanguage.get(x).getId();
                shortString[x][1] = this.descriptionLanguage.get(x).getValue();
                // shortString[x][0]=this.idlang;
            }
            this.descriptionLanguage.clear();
            nodoActualCategories.setMeta_description(shortString);
        }
        if ("meta_keywords".equals(qName)) {
            String[][] shortString = new String[this.descriptionLanguage.size()][2];
            for (int x = 0; this.descriptionLanguage.size() > x; x++) {
                shortString[x][0] = this.descriptionLanguage.get(x).getId();
                shortString[x][1] = this.descriptionLanguage.get(x).getValue();
                // shortString[x][0]=this.idlang;
            }
            this.descriptionLanguage.clear();
            nodoActualCategories.setMeta_keywords(shortString);
        }
        if ("description".equals(qName)) {
            String[][] shortString = new String[this.descriptionLanguage.size()][2];
            for (int x = 0; this.descriptionLanguage.size() > x; x++) {
                shortString[x][0] = this.descriptionLanguage.get(x).getId();
                shortString[x][1] = this.descriptionLanguage.get(x).getValue();
                // shortString[x][0]=this.idlang;
            }
            this.descriptionLanguage.clear();
            nodoActualCategories.setDescription(shortString);
        }

        if ("categories".equals(qName)) {
            if (this.associations == true) {
                String[] shortString = new String[this.categories.size()];
                for (int x = 0; this.categories.size() > x; x++) {
                    shortString[x] = this.categories.get(x).toString();
                    // shortString[x][0]=this.idlang;
                }
                nodoActualCategories.setCategories(shortString);
            }
        }
        if ("products".equals(qName)) {
            String[] shortString = new String[this.products.size()];
            for (int x = 0; this.products.size() > x; x++) {
                shortString[x] = this.products.get(x).toString();
                // shortString[x][0]=this.idlang;
            }
            nodoActualCategories.setProducts(shortString);
        }

        if ("category".equals(qName) && this.associations==false && this.categories!=null && this.products!=null) {
            this.nodos.add(this.nodoActualCategories);   
            this.categories = null;
            this.products = null;
        }
//            if (this.categories != null && this.products != null) {
//
//                this.nodos.add(nodoActualCategories);
//                this.contenido = this.nodoActualCategories.toXML();
//            }
            //this.associations = false;
            //nodoActualManufacturers.setId(contenido);
        //}
        if ("associations".equals(qName)) {
            this.associations = false;
            
            
        }

    }

    class langTemp {

        private String id;
        private String value;

        public void langTemp() {
        }

        public void langTemp(String id, String value) {
            this.setId(id);
            this.setValue(value);
        }

        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }

        public String getValue() {
            if (value == null) {
                value = "";
            }
            return value;
        }

        public void setValue(String value) {
            this.value = value;
        }
    }
}